package com.hieu.adapter;

import java.util.ArrayList;

import com.hieu.bean.Action;
import com.hieu.tripmanager.R;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ActionAdapter extends ArrayAdapter<Action> {
	Context context;
	int layoutResourceId;
	ArrayList<Action> mActionList;

	public ActionAdapter(Context context, int layoutResourceId,
			ArrayList<Action> data) {
		super(context, layoutResourceId, data);
		this.context = context;
		this.layoutResourceId = layoutResourceId;
		this.mActionList = data;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;
		ActionHolder holder = null;

		if (row == null) {
			LayoutInflater inflater = ((Activity) context).getLayoutInflater();
			row = inflater.inflate(layoutResourceId, parent, false);

			holder = new ActionHolder();
			holder.icon = (ImageView) row.findViewById(R.id.imgCategory);
			holder.time = (TextView) row.findViewById(R.id.txtTime);
			holder.description = (TextView) row
					.findViewById(R.id.txtDescription);

			row.setTag(holder);
		} else {
			holder = (ActionHolder) row.getTag();
		}

		Action action = mActionList.get(position);

		if (action.getCate().equals("C1"))
			holder.icon.setImageResource(R.drawable.bus_icon);
		else if (action.getCate().equals("C2"))
			holder.icon.setImageResource(R.drawable.food_icon);
		else if (action.getCate().equals("C3"))
			holder.icon.setImageResource(R.drawable.lodging_icon);
		else if (action.getCate().equals("C4"))
			holder.icon.setImageResource(R.drawable.rent_car_icon);
		else if (action.getCate().equals("C5"))
			holder.icon.setImageResource(R.drawable.see_icon);
		else if (action.getCate().equals("C6"))
			holder.icon.setImageResource(R.drawable.phone_icon);
		else if (action.getCate().equals("C7"))
			holder.icon.setImageResource(R.drawable.cart_icon);
		else if (action.getCate().equals("C8"))
			holder.icon.setImageResource(R.drawable.refreshment_icon);
		else if (action.getCate().equals("C9"))
			holder.icon.setImageResource(R.drawable.groceries_icon);
		else
			holder.icon.setImageResource(R.drawable.other_icon);

		holder.time.setText(action.getTime());
		holder.description.setText(action.getDescription());

		return row;
	}

	static class ActionHolder {
		ImageView icon;
		TextView time;
		TextView description;

	}

}
